<template>
    <div class="login">
        <div class="login-main">
            <h1>登录</h1>
            <div class="ipt">
                <!-- 实现登录页面和功能 -->
                <input type="text" placeholder="请输入用户名或手机号" v-model="user">
                <input type="text" placeholder="请输入密码" v-model="pwd">
                <input type="text" placeholder="注册时添加" v-model="token">
                <button @click="btnClick()">登录</button>
                <button @click="addClick()">注册</button>
            </div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import appAxios from '../../appAxios'
import {useStore} from 'vuex'
import {stateType} from '@/store'
import {ref} from 'vue'
import {useRouter} from 'vue-router'
import { computed } from '@vue/reactivity';
const store=useStore<stateType>()
const router=useRouter()
const user=ref('')
const pwd=ref('')
const token=ref('')
const list=computed(()=>store.state.list)
const btnClick=()=>{
    list.value.forEach(v=>{
        if(v.pwd===pwd.value&&v.user===user.value){
            store.commit('getToken',v)
            router.push('/user')
        }
    })
}
const addClick=async()=>{
    const resp=await appAxios.get('/api/add',{params:{user:user.value,pwd:pwd.value,token:token.value,id:Math.random().toString().slice(2)}})
    const {code}=resp.data
    if(code===200){
        store.dispatch('getLoginListAxios')
    }
}
</script>
<style lang="scss">
    .login{
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
    }
    .login-main{
        width: 300px;
        height: 400px;
        border: 1px solid #ccc;
        margin-top: 100px;
        display: flex;
        flex-direction: column;
    }
    .login-main h1{
        width: 100%;
        height: 60px;
        border-bottom: 1px solid #f2f2f2;
        display: flex;
        justify-content: center;
        align-content: center;
    }
    .ipt{
        width: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
    }
    .ipt input{
        width: 240px;
        height: 50px;
        margin: 0 30px;
    }
    .ipt button{
        width: 100px;
        height: 50px;
        margin: 0 100px;
    }
</style>